// pages/notice/notice.js
let app = getApp()
let yuming = app.globalData.yuming
const recorderManager = wx.getRecorderManager()
const innerAudioContext = wx.createInnerAudioContext()
var shuzong = []
var shu = []
const request = require('../../config/request');
Page({
  ...request,
  /**
   * 页面的初始数据
   */
  data: {
    yuming: yuming,
    heights: "",
    title: '通知消息',
    tonglist: [],
    currtab: 0,
    page: 1,
    form: 1,
    isshow: false,
    isyuyin: false,
    isSpeaking: false,
    jinghe_id:'',
    bofangurl: [],
    setInter: '',
    options: '',
    miao: 1,
    wenben: '', //文本内容
    wuyuyin: '',
    isbo: false, //是否播放
    er: 0,
    isbo2: false,
    isbo3: false,
    er2: 0,
    er3: 0,
    duration: '',
    jgweidu: 0,
    grweidu: 0,
    gongwu: false,
    total:''
  },
  // 手动输入
  kaiwen(e) {
    var id = e.currentTarget.dataset.id
    this.setData({
      isshow: !this.data.isshow,
      isyuyin: false,
      ids: id
    })
  },
  // 语音输入
  yuyinsr(e) {
    var id = e.currentTarget.dataset.id
    this.setData({
      isyuyin: !this.data.isyuyin,
      isshow: false,
      ids: id
    })
  },
  // 文本输入内容
  wenben(e) {
    this.setData({
      wenben: e.detail.value
    })
  },
  start(e) {
    this.setData({
      options: {
        duration: 60000, //录音的时长，单位 ms
        sampleRate: 44100, //采样率
        numberOfChannels: 1, //录音通道数
        encodeBitRate: 192000, //编码码率
        format: 'mp3', //音频格式
      }
    })
    console.log(this.data.options.duration)
    recorderManager.start(this.data.options)
    recorderManager.onStart(() => {
      console.log('录音开始')
    })
    var that = this
    this.data.setInter = setInterval(function () {
      var miaos = that.data.miao++
      // var sss = Math.floor(miaos / 1000 % 60);
      console.log(miaos)
      if (miaos == 60) {
        that.end()
      }
    }, 1000)
    this.setData({
      isSpeaking: true
    })
  },
  end: function (e) {
    var that = this;
    recorderManager.stop(); //先停止录音
    that.setData({
      miao: 1
    })
    clearInterval(this.data.setInter);
    this.setData({
      isSpeaking: false
    })
    recorderManager.onStop((res) => { //监听录音停止的事件
      console.log("监听录音停止事件", res)
      if (res.duration < 1000) {
        wx.showToast({
          title: '录音时间太短',
          icon: 'none'
        })
        return;
      } else {
        let time = res.duration
        var s = Math.floor(time / 1000 % 60);
        that.setData({
          duration: s
        })
        var tempFilePath = res.tempFilePath; // 文件临时路径
        console.log("文件临时路径", tempFilePath)
        // var sub = {url:tempFilePath,miao:s}
        // that.setData({
        //   bofangurl:this.data.bofangurl.concat(sub)
        // })
        wx.uploadFile({
          url: yuming + 'Besall/Upload', //上传服务器的地址
          filePath: tempFilePath, //临时路径
          name: 'file',
          header: {
            contentType: "multipart/form-data", //按需求增加
          },
          formData: null,
          success: function (res) {
            var data = JSON.parse(res.data)
            that.setData({
              bofangurl: data.httpUrl + data.imgUrl,
              wuyuyin: data.imgUrl,
            })
          },
          fail: function (err) {
            console.log(err.errMsg);
          }
        });
      }
    });
  },
  // 回复
  huifu() {
    var that = this
    this.myRequest(yuming + 'Besall/NoticeForReplySave', {
      shopId: wx.getStorageSync('shopId'),
      fid: that.data.ids,
      reply: that.data.wenben,
      Phonetic: that.data.wuyuyin,
      seconds: that.data.duration
    }, "POST").then(res => {
      wx.showToast({
        title: res.data.msg,
        icon: 'none'
      })
      that.lieb()
      that.setData({
        isshow: false,
        isyuyin: false,
        wenben: '',
        bofangurl: ''
      })
    })
  },
  bo(e) {
    var aaa = e.currentTarget.dataset.aaa
    console.log('开始播放')
    if (aaa == 0) {
      this.setData({
        isbo: !this.data.isbo
      })
    }
    if (aaa == 1) {
      this.setData({
        isbo2: !this.data.isbo2
      })
    }
    if (aaa == 2) {
      this.setData({
        isbo3: !this.data.isbo3
      })
    }
    var that = this
    var t1 = setInterval(function () {
      if (aaa == 0) {
        var num = that.data.er
        // console.log('监管')
      }
      if (aaa == 1) {
        var num = that.data.er2
        // console.log('我')
      }
      if (aaa == 2) {
        var num = that.data.er3
        // console.log('我')
      }
      num++
      if (aaa == 0) {
        that.setData({
          er: num
        })
        if (that.data.er > 2) {
          that.setData({
            er: 0
          })
        }
      }
      if (aaa == 1) {
        that.setData({
          er2: num
        })
        if (that.data.er2 > 2) {
          that.setData({
            er2: 0
          })
        }
      }
      if (aaa == 2) {
        that.setData({
          er3: num
        })
        if (that.data.er2 > 2) {
          that.setData({
            er3: 0
          })
        }
      }
    }, 300)
    var url = ''
    if (aaa == 0 || aaa == 1) {
      url = e.currentTarget.dataset.url[0]
    } else {
      url = e.currentTarget.dataset.url
    }
    innerAudioContext.src = url
    console.log(innerAudioContext.src)
    innerAudioContext.play()
    innerAudioContext.obeyMuteSwitch = false
    innerAudioContext.onEnded(() => {
      console.log("停止播放")
      innerAudioContext.stop()
      clearInterval(t1);
      if (aaa == 0) {
        that.setData({
          isbo: false,
          er: 0
        })
      }
      if (aaa == 1) {
        that.setData({
          isbo2: false,
          er2: 0
        })
      }
      if (aaa == 2) {
        that.setData({
          isbo3: false,
          er3: 0
        })
      }
    })
  },
  // 进入详情
  xq(e) {
    var id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/page_one/jianguanxq/jianguanxq?id=' + id + "&curr=" + this.data.currtab + "&title=通知消息详情",
    })
  },
  // 一键已读
  yijian() {
    var that = this
    this.myRequest(yuming + 'Besall/messageOneKeyRead', {
      shopId: wx.getStorageSync('shopId'),
      form: 1, //1我的通知 2监管提醒
    }, "POST").then(res => {
      setTimeout(function () {
        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        })
      }, 500)
      that.lieb();
      that.hongdian();
    })
  },
  // 查看更多
  lookmore() {
    shuzong = this.data.tonglist
    this.setData({
      page: (this.data.page) + 1
    })
    this.lieb()
  },
  // 列表
  lieb() {
    var that = this
    wx.showLoading({
      title: '加载中',
      icon: 'none'
    })
    this.myRequest(yuming + 'Besall/messageNoticeByUs', {
      shopId: wx.getStorageSync('shopId'),
      page: that.data.page,
      form: that.data.form, //1我的通知 2监管提醒
    }, "POST").then(res => {
      wx.hideLoading()
      shu = res.data.data
      that.setData({
        total:res.data.total
      })
      if (that.data.page != 1) {
        if (shu.length != 10) {
          wx.showToast({
            title: '没有更多数据了~',
            icon: "none"
          })
          that.setData({
            // tonglist: []
            gongwu: true,
          })
        }
      }
      that.setData({
        tonglist: shuzong.concat(shu)
      })
    })
  },
  // 列表
  liebbaoji() {
    var that = this
    wx.showLoading({
      title: '加载中',
      icon: 'none'
    })
    this.myRequest(yuming + 'Shopthat/shop_notice_list', {
      shopId: wx.getStorageSync('shopId'),
      page: that.data.page,
      NoticeType: 2, //1我的通知 2监管提醒
    }, "POST").then(res => {
      wx.hideLoading()
      shu = res.data.data
      that.setData({
        total:res.data.total
      })
      if (that.data.page != 1) {
        if (shu.length != 10) {
          wx.showToast({
            title: '没有更多数据了~',
            icon: "none"
          })
          that.setData({
            // tonglist: []
            gongwu: true,
          })
        }
      }
      that.setData({
        tonglist: shuzong.concat(shu)
      })
    })
  },
  // 红点
  hongdian() {
    var that = this
    this.myRequest(yuming + 'besall/messageHongdian', {
      shopId: wx.getStorageSync('shopId'),
    }, "POST").then(res => {
      console.log(res)
      that.setData({
        jgweidu: res.data.data.jg_count,
        grweidu: res.data.data.gr_count,
      })
    })
  },
  backs() {
    wx.navigateBack({
      delta: 0
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      heights: wx.getMenuButtonBoundingClientRect().top + 5,
      title: options.title
    })
    wx.hideTabBar();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      page: 1,
      gongwu: false,
      jinghe_id:wx.getStorageSync('jinghe_id')
    })
    shuzong = []
    if(wx.getStorageSync('jinghe_id') == 176){
        this.lieb()
    }else{
        this.liebbaoji()
    }
    this.hongdian()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
})